<template>
	<el-container>
		<el-aside width="200px" v-loading="showGrouploading">
			<el-container>
				<el-header>
					<el-input placeholder="输入关键字进行过滤" v-model="groupFilterText" clearable></el-input>
				</el-header>
				<el-main class="nopadding">
					<el-tree ref="officeSuppliesSort" class="menu" node-key="sortId" :data="officeSuppliesSortList" :props="groupsProps" :current-node-key="''" :highlight-current="true" :expand-on-click-node="false"
							 :filter-node-method="groupFilterNode" @node-click="groupClick">
						<template #default="{node, data}">
							<span class="custom-tree-node">
								<span class="label">{{ node.label }}</span>
								<span class="code">{{ data.code }}</span>
							</span>
						</template>
					</el-tree>
				</el-main>
			</el-container>
		</el-aside>
		<el-container>
			<el-header>
				<div class="left-panel">
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-input v-model="search.keyword" placeholder="物资名称" clearable></el-input>
						<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
					</div>
				</div>
			</el-header>
			<el-main class="nopadding">
				<scTable ref="table" :apiObj="apiObj" row-key="suppliesId"  stripe remoteSort>
					<el-table-column label="#" type="index" width="50"></el-table-column>
					<el-table-column label="办公用品名称" prop="suppliesName" width="200" sortable='custom' :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="物资分类" prop="sortName" width="100" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="品牌" prop="brand" width="150" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="规格型号" prop="model" width="200" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="可用数量" prop="quantity" width="100">
						<template #default="scope">
							{{scope.row.quantity}}/{{scope.row.unitName}}
						</template>
					</el-table-column>
					<el-table-column label="备注" prop="remark" width="300" :show-overflow-tooltip="true"></el-table-column>
					<el-table-column label="操作" fixed="right" align="right" width="100">
						<template #default="scope">
							<el-button-group>
								<el-button text type="primary" size="small" @click="applyOfficeSupplies(scope.row, scope.$index)">申请</el-button>
							</el-button-group>
						</template>
					</el-table-column>
				</scTable>
			</el-main>
		</el-container>
	</el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save=false"></save-dialog>
</template>
<script>

import saveDialog from "./save";

export default {
	name: 'Office Supplies Approval',
	components: {
		saveDialog
	},
	data() {
		return {
			apiObj: this.$API.office.officeSupplies.getApplyOfficeSuppliesList,
			dialog: {
				save: false,
			},
			showGrouploading: false,
			groupFilterText: '',
			fileList:[],
			officeSuppliesSortList: [],
			groupsProps: {
				label: "sortName",
				value: "sortId"
			},
			search: {
				keyword: null
			}
		}
	},
	watch: {
		groupFilterText(val) {
			this.$refs.officeSuppliesSort.filter(val);
		}
	},
	mounted() {
		this.getOfficeSuppliesSortGroup()
	},
	methods: {
		//加载树数据
		async getOfficeSuppliesSortGroup() {
			this.showGrouploading = true;
			var res = await this.$API.office.officeSupplies.getOfficeSuppliesSortTree.get();
			this.showGrouploading = false;
			this.officeSuppliesSortList = res.data;
		},
		//树过滤
		groupFilterNode(value, data) {
			if (!value) return true;
			return data.sortName.indexOf(value) !== -1;
		},
		//树点击事件
		groupClick(data) {
			this.$refs.table.reload({
				sortId: data.sortId
			})
		},
		//本地更新数据
		handleSuccess(data, mode) {
			this.$refs.table.refresh();

		},
		async handleSortSuccess() {
			var res = await this.$API.office.officeSuppliesSort.getOfficeSuppliesSortTree.get();
			if (res.code == 200) {
				this.officeSuppliesSortList = res.data;
			}
		},
		async applyOfficeSupplies(row, index) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open().setData(row)
			})
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
	}
}
</script>
<style scoped>
.custom-tree-node {
	/*display: flex;*/
	flex: 1;
	align-items: center;
	justify-content: space-between;
	font-size: 14px;
	padding-right: 24px;
	height: 100%;
}

.custom-tree-node .code {
	font-size: 12px;
	color: #999;
}

.custom-tree-node .do {
	margin-left: 10px;
	display: none;
}

.custom-tree-node:hover .code {
	display: none;
}

.custom-tree-node:hover .do {
	margin-left: 10px;
	display: inline-block;
}
</style>
